﻿var trunPageObj;//翻页组件对像
var fromEleIds = "#orderDate,#confirmDate,#settleDate,#oppPar,#mktCode,#arapBusiType,#curType,#arapAmt";
var isModify;//是否修改
$(initPage);
function initPage() {
	//初始化翻页组件
    trunPageObj =  $(".pageNext").turnPage(10, exportList);
    trunPageObj.gotoPage(1);//搜索第一页
    
    //重载数据
	$("#btnLoad").click(function() {
		reloadData();
	});
	
	//确认生成
	$("#btnCfm").click(function() {
		confirmGenData();
	});
	
	//对方机构为TA代码时显示输入框
	$("#oppPar")[0].onchange = function() {
		if (this.value == "TA") {
			$("#taCode").show();
		} else {
			$("#taCode").hide();
		}
	};
	
	
	//新增
	$("#btnAdd").click(function() {
		$("#btnAdd,#btnDelete").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled", false);
		$(fromEleIds).attr("disabled", false);
		
		clearValues();
	});
	
	
	//取消
	$("#btnCancle").click(function() {
		$("#btnSave,#btnDelete,#btnCancle").attr("disabled", true);
		$("#btnAdd").attr("disabled", false);
		$(fromEleIds).attr("disabled", true);
		clearValues();
	});
	
	//保存
	$("#btnSave").click(function() {
		saveOfCfm();
	});
	
	//删除
	$("#btnDelete").click(function() {
		delOfCfm();
	});
}

//重载数据
function reloadData() {
	if (!confirm("重载数据会还原所有操作，确定继续？")) {
		return;
	}
	$.post("m/of_cfm/load", {
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
//				alert("系统提示：操作成功！");
				location.reload();
			} 
		}, "json");
}

//确认生成
function confirmGenData() {
	if (!confirm("确认当日柜台入账数据都处理完毕，是否继续？")) {
		return;
	}
	$.post("m/of_cfm/confirm", {
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				alert("系统提示：操作成功！");
			} 
		}, "json");
}

//清除表单值
function clearValues() {
	$(fromEleIds).each(function() {
		var d_val = $(this).attr("default-value");
		$(this).val(d_val ? d_val : "");
		
	});

	$("tr","#data_list").attr("bgColor","#FFFFFF");
}

//保存
function saveOfCfm() {
	if($.validInput("orderDate", "交易日期", true, "\<\>\'\"", 10)){
		return false;
	}
	if($.validInput("confirmDate", "确认日期", true, "\<\>\'\"", 10)){
		return false;
	}
	if($.validInput("settleDate", "交收日期", true, "\<\>\'\"", 10)){
		return false;
	}
	if($.validInput("oppPar", "TA代码", true)){
		return false;
	}
	if($.validInput("mktCode", "市场代码", true)){
		return false;
	}
	if($.validInput("arapBusiType", "业务类型", true)){
		return false;
	}
	if($.validInput("curType", "币种", true)){
		return false;
	}
	if($.validNumber("arapAmt", "金额", true, 999999999999999)){
		return false;
	}

	$.post("m/of_cfm/add", {
		    orderDate :$.trim($("#orderDate").val()),
			confirmDate :$.trim($("#confirmDate").val()),
			settleDate :$.trim($("#settleDate").val()),
			arapType :$.trim($("#arapType").val()),
			oppPar :$.trim($("#oppPar").val()),
			mktCode :$.trim($("#mktCode").val()),
			arapBusiType :$.trim($("#arapBusiType").val()),
			curType:$.trim($("#curType").val()),
			arapAmt:$.trim($("#arapAmt").val())
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				alert("系统提示：保存成功！");
				$("#btnCancle").click();
				location.reload();
			} 
		}, "json");
}

//删除
function delOfCfm() {
	if (!confirm("确定要删除记录吗？")) {
		return;
	}
	$.post("m/of_cfm/delete", {
		    id :$.trim($("#hidId").val())
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				$("#btnCancle").click();
				location.reload();
			} 
		}, "json");
}

function queryList(){
	var pageno=trunPageObj.getCurrentPageNo();//当前第几页
	var pagecount=trunPageObj.getPageCount();//一页多少条
	var busiDate = $("#busiDate").val();
	$.post("m/of_cfm/query", {
		pageNo: pageno,
		pageSize : pagecount,
		busiDate : busiDate
	}, function(data) {
		if ($.checkErrorMsg(data) ) {
			var fileList = [];
			if(data.ofConfirmProcPage && data.ofConfirmProcPage.dataList){
				var dataList = data.ofConfirmProcPage.dataList;
				$(dataList).each(function(i) {
					fileList.push("<tr class='table_td_font' bgColor='#ffffff' onclick='selectTR(this)' objid='" + dataList[i].id + "'>");
					fileList.push("<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].formatOrderDate + "</td>");
					fileList.push("<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].formatConfirmDate + "</td>");
					fileList.push("<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].formatSettleDate + "</td>");
					fileList.push("<td align='left'  height='25px' class='queryList_linebg' value='" + dataList[i].arapType + "'>" + dataList[i].arapTypeText + "</td>");
					fileList.push("<td align='left'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].oppPar + "'>" + dataList[i].oppPar + "</td>");
					fileList.push("<td align='left'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].fundCode + "'>" + dataList[i].fundCode + "</td>");
					fileList.push("<td align='left'  height='25px' class='queryList_linebg' value='" + dataList[i].mktCode + "'>" + dataList[i].mktCodeText + "</td>");
					fileList.push("<td align='left'  height='25px' class='queryList_linebg' value='" + dataList[i].arapBusiType + "'>" + dataList[i].arapBusiTypeText + "</td>");
					fileList.push("<td align='center'  height='25px' class='queryList_linebg' nowrap value='" + dataList[i].curType + "'>" + dataList[i].curTypeText + "</td>");
					fileList.push("<td align='right'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].arapAmtText + "</td>");
					fileList.push("<td align='left'  height='25px' class='queryList_linebg' nowrap>");
					if (dataList[i].arapType == '00') {
						fileList.push("<a href='javascript:modArapType(\"" + dataList[i].id + "\",\"99\")'>交收</a> <a href='javascript:modArapType(\"" + dataList[i].id + "\",\"03\")'>生成</a>");
					} else if (dataList[i].arapType == '02' || dataList[i].arapType == '03') {
						fileList.push("<a href='javascript:modArapType(\"" + dataList[i].id + "\",\"99\")'>交收</a>");
					}
					fileList.push("<br><a href='javascript:viewList(\"" + dataList[i].id + "\")'>查看明细</a>");
					fileList.push("</td>");
					fileList.push("</tr>");
				});
			}
			$("#data_list").html(fileList.join(""));
			trunPageObj.setPageData(data.ofConfirmProcPage);
		} 
	}, "json");
}

function selectTR(obj) {
	$(obj).attr("bgColor","#FAEBD7").siblings().attr("bgColor","#FFFFFF");
	$("#hidId").val($(obj).attr("objid"));
	$("#btnSave,#btnCancle").attr("disabled", true);
	$("#btnAdd,#btnDelete").attr("disabled", false);
	$(fromEleIds).attr("disabled", true);
}


function modArapType(id, arapType) {
	if (!confirm(arapType == "99" ? "确认该记录已交收？" : "确定要生成已付客户未收对方机构(03)数据？")) {
		return;
	}
	
	$.post("m/of_cfm/modify", {
		    id : id,
			arapType : arapType
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				location.reload();
			} 
		}, "json");
}

function viewList(id) {
	var url = BASE_PATH + "m/of_cfm/view?id=" + id;
	window.showModalDialog(url,"","dialogWidth:580px;dialogHeight:360px;help:no;center:yes;status:no");
}

function exportList() {
	var obj = window.showModalDialog(BASE_PATH + "page/export_select.jsp","","dialogWidth:200px;dialogHeight:80px;help:no;center:yes;status:no");
	if (obj) {
		window.open("m/of_cfm/export?busiDate=" + $.trim($("#busiDate").val()) + "&expType=" + obj);
	}
}